<template>
    <div>
        <div class="layout-route" v-show="!editData.visible">
            <div class="card card-search">
                <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                    <el-form-item label="店铺" prop="shop_id">
                        <el-select v-model="queryParams.shop_id" placeholder="请选择店铺" filterable clearable style="width: 240px">
                            <el-option v-for="(dict, index) in dict.shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="产品类型" prop="product_type">
                        <el-select v-model="queryParams.product_type" placeholder="请选择产品类型" clearable style="width: 240px">
                            <el-option v-for="(dict, index) in dict.productTypeList" :key="index" :label="dict.type_name" :value="dict.type_id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="SPU" prop="spu">
                        <el-input v-model.trim="queryParams.spu" placeholder="请输入SPU" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="产品变体" prop="product_variant_type">
                        <el-select v-model="queryParams.product_variant_type" placeholder="全部" clearable style="width: 240px">
                            <el-option v-for="(dict, index) in dict.productVariantTypeList" :key="index" :label="dict.label" :value="dict.value"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="产品标题" prop="product_title">
                        <el-input v-model.trim="queryParams.product_title" placeholder="请输入产品系列" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="创建时间" prop="create_time">
                        <el-date-picker v-model="queryParams.create_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label=" ">
                        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>

            <div class="card">
                <el-row :gutter="10" class="mb8">
                    <!-- <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" @click="gotoDetail">添加</el-button>
                </el-col> -->
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>
                <el-tabs v-model="queryParams.listing_status" type="border-card" @tab-click="handleQuery">
                    <el-tab-pane label="全部" :name="undefined"></el-tab-pane>
                    <el-tab-pane v-for="item in dict.listingStatus" :key="item.value" :label="item.label" :name="item.value.toString()"></el-tab-pane>
                </el-tabs>
                <div class="table-wrap">
                    <el-table v-loading="loading" :data="tableData" row-key="id" border script>
                        <el-table-column label="图片" min-width="100">
                            <template slot-scope="scope">
                                <ImageUploadImg v-model="scope.row.basic_image" :readOnly="true" :popoverDisabled="false" :hoverType="'hover'">
                                    <div slot="hover">
                                        <div style="cursor: pointer;"><i class="el-icon-search"></i>找货源 <el-tag type="warning" @click="toNewWebsite(scope.row.supply_url)">1688</el-tag></div>
                                        <div style="margin-top: 4px">
                                            <el-image :src="scope.row.basic_image" style="width: 350px; height: 350px;"></el-image>
                                        </div>
                                    </div>
                                </ImageUploadImg>
                            </template>
                        </el-table-column>
                        <el-table-column label="店铺名称" prop="shop_name" min-width="100"> </el-table-column>
                        <el-table-column label="SPU" prop="spu" min-width="60">
                            <template slot-scope="scope">
                                <el-link type="primary" @click="showSpuListDialog(scope.row)">{{ scope.row.spu }}</el-link>
                            </template>
                        </el-table-column>
                        <el-table-column label="产品" prop="product_title" min-width="300">
                            <template slot-scope="scope">
                                <div>
                                    <span>{{ scope.row.product_title }}</span> 
                                    <div v-if="scope.row.product_shopify_id">
                                        <span style="color: #11a983;cursor: pointer;" @click="toNewWebsite(scope.row.product_shopify_url)">{{ scope.row.product_shopify_id }}</span>
                                    </div>
                                </div>
                                <div v-if="scope.row.remark" :style="{color: scope.row.remark_color}">
                                    备注：{{ scope.row.remark }}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="分类" prop="product_type_value" min-width="120" show-overflow-tooltip></el-table-column>
                        <el-table-column label="变体类型" prop="product_variant_type_value" min-width="120" show-overflow-tooltip></el-table-column>

                        <el-table-column label="创建时间：" prop="spu" min-width="120">
                            <template slot-scope="scope">
                                {{ scope.row.create_time }}
                            </template>
                        </el-table-column>
                        <el-table-column label="更新时间：" prop="spu" min-width="120">
                            <template slot-scope="scope">
                                {{ scope.row.update_time }}
                            </template>
                        </el-table-column>
                        <el-table-column label="刊登时间：" prop="spu" min-width="120">
                            <template slot-scope="scope">
                                {{ scope.row.update_time }}
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" width="100" fixed="right">
                            <template slot-scope="scope">
                                <div><el-button type="text" icon="el-icon-edit" @click="productEdit(scope.row)">编辑</el-button></div>
                                <div><el-button type="text" icon="el-icon-edit-outline" @click="productPublish(scope.row)" v-if="['1', '5'].includes(queryParams.listing_status)">发布</el-button></div>
                                <div><el-button type="text" icon="el-icon-down" @click="changeStatus(scope.row, 'down')" v-if="['2', '4'].includes(queryParams.listing_status)">下架产品</el-button></div>
                                <div><el-button type="text" icon="el-icon-up" @click="changeStatus(scope.row, 'up')" v-if="['3'].includes(queryParams.listing_status)">上架产品</el-button></div>
                                <div><el-button type="text" icon="el-icon-document-add" @click="addRemark(scope.row)" v-if="['1', '2', '3', '4', '5'].includes(queryParams.listing_status)">添加备注</el-button></div>
                                <div><el-button type="text" icon="el-icon-delete" @click="productDelete(scope.row)" v-if="['1', '2', '3', '4', '5'].includes(queryParams.listing_status)">删除产品</el-button></div>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList" />
            </div>
            <shopRemarkDialog v-model="remarkDialog.visible" :params="remarkDialog.data" @success="handleQuery"></shopRemarkDialog>

            <SpuListDialog v-model="spuListDialog.visible" v-if="spuListDialog.visible" :params="spuListDialog.data"></SpuListDialog>
        </div>
        <product-detail v-show="editData.visible" :params="editData.data" @success="handleQuery" @cancel="editData.visible=false"></product-detail>
    </div>
</template>
<script>
import { getAttrList } from '@/api/product'
import { getListingShopList, getListingShop, shopSendPublish, shopDelete, shopUp, shopDown} from '@/api/listing'
import { parseTime } from '@/utils/quickadmin'
import { PRODUCT_VARIANT_TYPE, LISTING_STATUS } from '../constant'
import shopRemarkDialog from './components/shopRemarkDialog.vue'
import SpuListDialog from './components/spuListDialog.vue'
import ProductDetail from './detail.vue'
export default {
    name: 'ProductOnLineList',
    components: { shopRemarkDialog, SpuListDialog, ProductDetail },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],

            // 状态数据字典
            dict: {
                shopList: [],
                productVariantTypeList: PRODUCT_VARIANT_TYPE,
                listingStatus: LISTING_STATUS,
            },
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                listing_status: undefined,
                shop_id: undefined,
                spu: undefined,
                product_type: undefined,
                product_variant_type: undefined,
                product_title: undefined,
                create_time: [],
            },
            remarkDialog: {
                visible: false,
                data: {},
            },

            spuListDialog: {
                visible: false,
                data: {},
            },
            editData: {
                visible: false,
                data: {},
            },
        }
    },
    async created() {
        this.loading = true
        try {
            const productTypeData = await getAttrList()
            this.dict.productTypeList = productTypeData?.data ?? []
            const shopData = await getListingShop()
            this.dict.shopList = shopData?.data ?? []
        } catch (error) {
            this.loading = false
        }
        this.getList()
    },
    methods: {
        productEdit(row) {
            this.editData.visible = true
            this.editData.data = row
            this.editData.data.isAll = this.queryParams.listing_status < 1
        },
        productPublish(row) {
            shopSendPublish({id: row.id}).then(res=>{
                this.$message.success(res.msg)
                this.handleQuery()
            })
        },
        productDelete(row) {
            this.$confirm('是否确认删除?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(async () => {
                    await shopDelete({ id: row.id, shop_id: row.shop_id })
                    this.getList()
                    this.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        changeStatus(row, status) {
            const params = { id: row.id, shop_id: row.shop_id }
            const request = status=='up' ? shopUp : shopDown
            request(params).then(res=>{
                this.$message.success(res.msg)
                this.handleQuery()
            })
        },
        addRemark(row) {
            this.remarkDialog.visible = true
            this.remarkDialog.data = row
        },
        toNewWebsite(url) {
            if(!url) {
                return
            }
            window.open(url,"_blank")
        },
        copyText(string) {
            // navigator.clipboard.writeText 该方法需要在安全域下才能够使用，比如：https 协议的地址、127.0.0.1、localhost
            navigator.clipboard
                .writeText(string)
                .then(() => {
                    this.$message.success('复制成功')
                })
                .catch(err => {
                    // 复制失败
                    console.info('复制失败')
                })
        },

        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            getListingShopList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 搜索按钮操作
        handleQuery2() {
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },

        showSpuListDialog(row) {
            this.spuListDialog.visible = true
            this.spuListDialog.data = row
        },
    },
}
</script>
<style scoped>
.custom-select .el-select-dropdown__item {
    max-width: 450px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.border-cell {
    padding: 0 !important;
}
.border-cell .cell {
    padding: 0 !important;
}
.border-cell-item {
    padding: 10px;
    border-bottom: 1px solid #dfe6ec;
}
.border-cell-item:last-child {
    border-bottom: 0;
}
.el-tabs--border-card {
    box-shadow: none;
    border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.table-wrap {
    padding: 15px;
    border-bottom: 1px solid #dfe4ed;
    border-left: 1px solid #dfe4ed;
    border-right: 1px solid #dfe4ed;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
</style>
